using GameFramework;
using GameFramework.Resource;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityGameFramework.Runtime;

namespace Map
{
    public class MapManager : MonoBehaviour
    {
        public readonly string UnitPrefabPath = Application.dataPath + "/MyGameDemo/Prefabs/MapUnit";
        public List<MapFloorUnit> mapUnitObjs = new List<MapFloorUnit>();
        private IResourceManager m_ResourceManager;
        private LoadAssetCallbacks loadAssetCallbacks;

        bool isResoureLoaded = false;

        private void ResourceLoaded()
        {
            m_ResourceManager.LoadAsset(UnitPrefabPath, new LoadAssetCallbacks((assetName, asset, duration, userData) =>
            {
                isResoureLoaded = true;
                Log.Info("load asset {0} success", assetName);
            },
            (assetName, status, errorMessage, userData) =>
            {
                isResoureLoaded = false;
                Log.Error("Can not load asset '{0}' with error message '{1}'.", assetName, errorMessage);
            }
            ));
        }
        
        public void InitMap()
        {
            if (!isResoureLoaded)
            {
                Log.Error("Resource not load");
                return;
            }
            InitFloor();
        }

        private void InitFloor()
        {
            MapFloorUnit mapFloorUnit = new MapFloorUnit();
            mapFloorUnit.InitFloor(FloorEnum.FirstFloor);            
        }

        public void SetResourceManager()
        {
            if (m_ResourceManager == null)
            {
                throw new GameFrameworkException("Resource manager is invalid.");
            }

            m_ResourceManager = GameFrameworkEntry.GetModule<IResourceManager>();

            ResourceLoaded();
        }

        // Start is called before the first frame update
        void Start()
        {
            SetResourceManager();
            InitMap();
        }

        // Update is called once per frame
        void Update()
        {

        }
    }
}

